// appointment.js
var app = getApp();
var util = require('../../utils/util.js')
Page({
  data: {
    apis: app.data.ipconfig,
    ISimg: true,
    ISimg2: false,
    selPs: 2,
    selIcon: 'yes-sel',
    selIcon2: 'no-sel',
    time: '选择时间',
    date: '选择日期',
    isShow: false,
    isShow2: false,
    yhq: '立即使用',
    carNum: 1,
  },
  onLoad: function (param) {
    console.log(util.formatTime(new Date).substring(0, 10));
    console.log(util.formatTime(new Date).substring(11, 17));
    var that = this;
    //console.log(param);
    if (param.dataname1 == 'undefined') {
      param.dataname1 = ' ';
    }
    if (param.dataname2 == 'undefined') {
      param.dataname2 = ' ';
    }
    if (param.dataname3 == 'undefined') {
      param.dataname3 = ' ';
    }
    if (param.dataname4 == 'undefined') {
      param.dataname4 = ' ';
    }
    that.setData({
      id: param.id,
      SumMoney: param.SumMoney,
      shopdj: param.shopdj,
      shopCount: param.shopCount,
      dataname1: param.dataname1,
      dataname2: param.dataname2,
      dataname3: param.dataname3,
      dataname4: param.dataname4,
      childspec1: param.childspec1,
      childspec2: param.childspec2,
      childspec3: param.childspec3,
      childspec4: param.childspec4,
      shopname: param.shopname,
      shoppic: param.shoppic,
      thisDate: util.formatTime(new Date).substring(0, 10),
      thisTime: util.formatTime(new Date).substring(11, 17)
    });

    wx.request({
      url: that.data.apis + '/wxxiaochengxu/goodsApi/byDetailOne',
      data: {
        appid: app.data.appid,
        openid: wx.getStorageSync('openid'),
        SumMoney: param.SumMoney
        //SumMoney:700
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data.res.shitaddr + "****");
        var flag = false;
        if (res.data.res.shitaddr == 0) {
          flag = false;
        } else {
          flag = true;
        }
        that.setData({
          addr: res.data.res.shitaddr,
          flag: flag,
          addrid: res.data.res.shitaddr.userShitoAddressId,
          yf: res.data.res.fire
        });
      }
    })

  },
  carAdd: function (event) {
    var that = this;
    //var shopPrice = 168;
    var con = that.data.carNum + 1;
    var key = 'carNum';
    var obj = {};
    if (con > 25) {
      obj[key] = 1;
    } else {
      obj[key] = con;
    }
    that.setData(obj);
    that.setData({
      numcount: con
    })
  },
  carReduce: function (event) {
    var that = this;
    var con = that.data.carNum;
    var key = 'carNum';
    var obj = {};
    if (con <= 1) {
      obj[key] = 1;
      that.setData(obj);
      that.setData({
        numcount: con
      })
    }
    else {
      con--;
      obj[key] = con;
      that.setData(obj);
      that.setData({
        numcount: con
      })
    }
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
  isimg: function () {
    var that = this;
    this.setData({
      selIcon: 'yes-sel',
      selIcon2: 'no-sel',
      selPs: 2
    });
    console.log(that.data.selPs);
  },
  isimg2: function () {
    var that = this;
    this.setData({
      selIcon: 'no-sel',
      selIcon2: 'yes-sel',
      selPs: 1
    });
    console.log(that.data.selPs);
  },
  selother: function () {
    var that = this;
    wx.request({
      url: that.data.apis + '/wxxiaochengxu/userinfoApi/userAddress', //仅为示例，并非真实的接口地址
      data: {
        state: 1,
        appid: app.data.appid,
        openid: wx.getStorageSync('openid')
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res);
        that.setData({
          addressList: res.data.res
        })
      }
    })
    that.setData({
      isShow: (!that.data.isShow)
    })
  },
  closeView: function () {
    var that = this;
    that.setData({
      isShow: false,
      isShow2: false
    })
  },
  selAdd: function (e) {
    var that = this;
    var addid = e.currentTarget.id;
    wx.request({
      url: that.data.apis + '/wxxiaochengxu/goodsApi/byDetailaddr',
      data: {
        appid: app.data.appid,
        openid: wx.getStorageSync('openid'),
        SumMoney: that.data.SumMoney,
        userShitoAreasId: addid
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          addr: res.data.res.shitaddr,
          yf: res.data.res.fire,
          selid: addid,
          addrid: addid,
          flag: true
        });
      }
    });
  },
  useyhq: function () {
    var that = this;
    wx.request({
      url: that.data.apis + '/wxxiaochengxu/goodsApi/userfavorables',
      data: {
        appid: app.data.appid,
        openid: wx.getStorageSync('openid'),
        SumMoney: that.data.SumMoney,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res);
        that.setData({
          yhqData: res.data.res
        });
      }
    });

    that.setData({
      isShow2: (!that.data.isShow2)
    })
  },
  selyhq: function (e) {
    var that = this;
    var addid = e.currentTarget.id;
    that.setData({
      yhqid: addid,
      yhq: '立减' + e.currentTarget.dataset.yhqmoney + '元'
    });
  },
  inputName: function (e) {
    this.name = e.detail.value;
  },
  inputPhone: function (e) {
    this.phone = e.detail.value;
  },
  addrNew: function () {
    wx.navigateTo({
      url: '../../pages/addAddress/index?state=0'
    });
  },
  submitOrder: function () {
    var that = this;
    var selPs = that.data.selPs;//选择的提取方式
    var date = that.data.date;//选择的日期
    var time = that.data.time;//选择的时间
    var shrid = that.data.addrid;//收货人ID
    var dgrname = this.name;//订购人名字
    var dgrphone = this.phone;//订购人电话
    var yf = that.data.yf;//商品运费
    var yhqid = that.data.yhqid;//使用的优惠券id

    if (date == '选择日期' || time == '选择时间') {
      console.log('请先选择配送时间');
      wx.showModal({
        title: '提示消息',
        content: '请先选择配送时间！',
        showCancel: false
      })
    } else if (shrid == undefined && selPs == 2) {
      console.log('请先选择收货人');
      wx.showModal({
        title: '提示消息',
        content: '请先选择收货人！',
        showCancel: false
      })
    } else if (dgrname == undefined) {
      console.log('请先填写订购人名字');
      wx.showModal({
        title: '提示消息',
        content: '请先填写订购人名字！',
        showCancel: false
      })
    } else if (dgrphone == undefined) {
      console.log('请先填写订购人电话');
      wx.showModal({
        title: '提示消息',
        content: '请先填写订购人电话！',
        showCancel: false
      })
    } else {
      if (yhqid == undefined) {
        yhqid = 0;
      }
      var goodsJson = [{
        "goodsDetailUrl": that.data.shoppic,
        "goodsGoodsId": that.data.id,
        "goodsSpecificationoChildId": that.data.childspec1 + ',' + that.data.childspec2 + ',' + that.data.childspec3 + ',' + that.data.childspec4 + ',',
        "goodsGoodsIdCount": that.data.shopCount,
        "goodsGoodrealPrice": that.data.shopdj,
        "goodsName": that.data.shopname
      }];
      wx.request({
        url: that.data.apis + '/wxxiaochengxu/ordersApi/confirmOrder',
        data: {
          appid: app.data.appid,
          openid: wx.getStorageSync('openid'),
          goodOrdersSendMethod: selPs,
          goodOrdersDeliveryDate: date,
          goodOrdersDeliveryTime: time,
          name: dgrname,
          tel: dgrphone,
          userShitoAddressId: shrid,
          goodTransPrice: yf,
          goodsJson: goodsJson,
          favorableId: yhqid
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res);
          console.log(res.data.status);
          if (res.data.status == 200) {
            wx.redirectTo({
              url: '../../pages/orderEnd/index?goodOrdersNo=' + res.data.res
            });
          } else {
            wx.showToast({
              title: '订单生成失败',
              icon: 'loading',
              duration: 2000
            })
            wx.navigateBack({ delta: 2 })
          }
          /*wx.navigateTo({
              url:'../../pages/orderEnd/index?goodOrdersNo='+res.data.res
          });*/
        }
      });
    }
  }
})